<?php
/**
 * Created by PhpStorm.
 * User: Adminlmcqistrator
 * Date: 2017/3/20 0020
 * Time: 09:26
 */
namespace app\Common;

use think\Controller;

//前台控制初始化
class HomeBase extends Controller {
	protected $user;
	protected $userToken;
	private static $handler;
	protected $set;

	/**
     * 空操作
     * @return [type] [description]
     */
	public function _empty() {
		$this->redirect('Index/index');
	}

	/**
     * 验证登陆
     * @return [type] [description]
     */
	public function __construct()
	{
		header('Content-type:text/html; charset=utf-8');
		$this->set = ['type'=>'redis','host'=>'127.0.0.1','port'=>6379,];
		parent::__construct();
        if (!isset($_SERVER['HTTP_TOKEN']))
        {
            return json_error('token不能为空');
            die;
        }
		$token = $_SERVER['HTTP_TOKEN'];

		if (empty($token)) 
		{
			return json_error('token不能为空');
		}
		$user = db('user')->where(['user_token' => $token])->find();
		if (!$user) 
		{
			return json_error('token不正确');
		}
		 $this->user = $user;
	}




    // redis 连接
    public static function handler()
    {
        if(!self::$handler){
            self::$handler = new \Redis();
            self::$handler -> connect('127.0.0.1','6379');
        }
        return self::$handler;
    }

    // redis 获取
    public static function getRedis($key)
    {
        $value = self::handler() -> get($key);
        $value_serl = @unserialize($value);
        if(is_object($value_serl)||is_array($value_serl)){
            return $value_serl;
        }
        return $value;
    }

    // redis 存储
    public static function setRedis($key,$value)
    {
        if(is_object($value)||is_array($value)){
            $value = serialize($value);
        }

        return self::handler() -> set($key,$value);
    }


    // 是否进行实名认证
    public function isPass($userId)
    {
        $uPass      = M('authentication')->where(['user_id' => $userId,'is_pass' => 1])->find();
        $uPass ? '' : $this->json_error('请先进行实名认证！');
    }
}